// pages/product/product.js
const app = getApp()

import {
    getgoodsDetail,
    getRelatedGoods,
    getCartTagData,
    addToCart
} from "../../request/request"
import {
    checkLogin
} from '../../utils/utils';
Page({

    data: {
        goodsid: '',
        attribute: [],
        gallery: [],
        info: [],
        issue: [],
        productList: [],
        show: false, //popupo显示隐藏
        relatedGoodsList: [], //相关产品列表
        cartNum: null, //购物车数量
        isCollection: false, //是否搜藏
        goodsCount: null, //商品数量
        stepNum: 1, //步进器数量
    },
    //显示弹出
    showPopup() {
        this.setData({
            show: true
        })

    },
    //关闭弹出
    onClose() {
        this.setData({
            show: false
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // console.log(options);
        this.setData({
            goodsid: options.goodsid.toString()
        })
        //发起获取购物车数量信息
        getCartTagData(

        ).then(res => {
            if (res.errno != 0) return
            this.setData({
                cartNum: res.data.cartTotal.goodsCount
            })
        })
        //发起获取相关产品数据请求
        getRelatedGoods(this.data.goodsid).then(res => {
            if (res.errno != 0) return
            this.setData({

                relatedGoodsList: res.data.goodsList
            })
        })
        //发送获取商品详细请求
        getgoodsDetail(this.data.goodsid).then(res => {

            let {
                attribute,
                gallery,
                info,
                issue,
                productList
            } = res.data

            info.goods_desc = info.goods_desc.replace(/<img/g, "<img class='product_img'") //处理富文本数据加类名，方便修改样式 （解决富文本图片空白）
            wx.setNavigationBarTitle({
                title: info.name + '商品详情',
            })
            this.setData({
                attribute: attribute,
                gallery: gallery,
                info: info,
                issue: issue,
                productList: productList,
            })

        })


    },
    //点击收藏
    clickCollection() {
        if (!checkLogin(true)) return
        this.setData({
            isCollection: !this.data.isCollection
        })
    },
    //加入购物车请求
    addToCartPort() {
        addToCart({
            goodsId: this.data.productList[0].goods_id,
            productId: this.data.productList[0].id,
            number: this.data.stepNum
        }).then(res => {
            if (res.errno == 400) {
                app.Notify({
                    type: "danger",
                    message: '库存不足'
                })
            }
            if (res.errno != 0) return
            this.setData({
                cartNum: res.data.cartTotal.goodsCount
            })
        })
    },
    //加入购物车
    async addToCartFn() {
        if (!checkLogin(true)) return

        //发送加入购物车请求
        if (!this.data.show) {
            //显示弹出框
            this.setData({
                show: true
            })
            return
        }

        if (this.data.show) {

            await this.addToCartPort()
            this.setData({
                stepNum: 1,
                show: false
            })

            app.Notify({
                type: "success",
                message: '添加购物车成功'
            })

        }


    },
    //立即购买
    buyNow() {
        if (!checkLogin(true)) return
        this.addToCartPort()
        wx.switchTab({
            url: '/pages/car/car',
        })
    },
    //步进器事件
    onChange(e) {

        this.setData({
            stepNum: e.detail
        })
    },
    //跳转到购物车
    ToCart() {
        if (!checkLogin(true)) return
        wx.switchTab({
            url: '/pages/car/car',
        })
    }


})